package com.hserver.beetlsql.service;

import com.hserver.beetlsql.bean.User;
import com.hserver.beetlsql.dao.UserDao;
import com.hserver.beetlsql.dao.UserDao1;
import com.hserver.beetlsql.dao.UserDao2;
import net.hserver.plugins.beetlsql.tx.Tx;
import org.beetl.sql.core.SQLManager;
import cn.hserver.core.ioc.annotation.Autowired;
import cn.hserver.core.ioc.annotation.Bean;
import cn.hserver.core.server.exception.BusinessException;

import java.util.Date;
import java.util.List;

@Bean
public class UserService {

    //默认的主从
    @Autowired
    private UserDao userDao;

    //数据源1
    @Autowired("data1")
    private UserDao1 userDao1;

    //数据源2
    @Autowired("data2")
    private UserDao2 userDao2;

    //默认的主从
    @Autowired
    private SQLManager sqlManager;

    /**
     * 事务测试
     *
     * @return
     */
    @Tx(rollbackFor = ArithmeticException.class,timeoutMillisecond = 1000)
    public boolean update() {
        System.out.println("我的："+Thread.currentThread().getName());
        userDao.update(2222, 1);
//        System.out.println(1 / 0);
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * sqlmanager 默认在容器中，可以直接进行获取然后使用
     *
     * @return
     */
    public boolean add() {
        User user = new User();
        user.setName("777");
        user.setAge(20);
        user.setCreateDate(new Date());
        return sqlManager.insert(user) > 0;
    }

    /**
     * Mapper你的dao导入使用
     *
     * @return
     */
    public List<User> select() {
        return userDao1.select();
    }

}
